/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fastaconverter.triplet;

import com.compomics.fastaconverter.DNAtoAAConverter;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Kenneth
 */
public class TripletFactory {

    private static HashMap<String, String[]> tripletFactoryMap = new HashMap<String, String[]>();

    public static void addToFactory(String triplet, String[] properties) {
        tripletFactoryMap.put(triplet, properties);
    }

    public static void initialize() {

        try {
            URL urlToDictionary = new DNAtoAAConverter().getClass().getResource("/DNAtoAmino.tsv");
            InputStream fstream = urlToDictionary.openStream();
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                String[] parts = strLine.split("\t");
                TripletFactory.addToFactory(parts[0].replace(" ", ""), parts);
            }
            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }


    }

    public static String getTripletSingleLetter(String triplet) {
        String singleLetter = "";
        try {
            singleLetter = tripletFactoryMap.get(triplet)[1];
            if (singleLetter.isEmpty()) {
                singleLetter = "X";
            }
        } catch (NullPointerException e) {
            singleLetter = "X";
        }
        return singleLetter;
    }

    public static String getTripletThreeLetter(String triplet) {
        return tripletFactoryMap.get(triplet)[2];
    }

    public static String getTripletName(String triplet) {
        return tripletFactoryMap.get(triplet)[3];
    }
}
